package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.*;
import com.acme.bankapp.exceptions.bank.*;

/**
 * Class provides service functionality
 * @author alex
 */
public class BankService {
	/**
	 * @param bank the bank
	 * @param name the client name
	 * @param gender the client gender
	 * @param balance the client account balance
	 * @param overdraft the client account overdraft
	 * @throws ClientsMaxException 
	 * @throws ClientExistsException 
	 */
	public static void addClient(Bank bank, String name, Gender gender, double balance, double overdraft) throws ClientsMaxException, ClientExistsException {
		Account account;
		if (overdraft > 0) {
			account = new CheckingAccount(balance, overdraft);
		} else {
			account = new SavingAccount(balance);
		}
		bank.addClient(new Client(account, name, gender));
	}
	
	/**
	 * @param bank the bank
	 * @param clientId the client id
	 * @return the client account
	 */
	public static Account getClientAccount(Bank bank, int clientId) {
		if (bank.getClient(clientId) != null) {
			return bank.getClient(clientId).getAccount();
		} else {
			return null;
		}
	}
	
	/**
	 * Prints on console the clients list
	 * @param bank the bank
	 */
	public static void printClientsList(Bank bank) {
		Client client;
		for (int i = 0; i < bank.getClientsCount(); i++) {
			client = bank.getClient(i);
			System.out.println(client.getGender().getSalutation() + client.getName());
		}
	}
	
	/**
	 * Prints on console the accounts list
	 * @param bank the bank
	 */
	public static void printAccountsList(Bank bank) {
		for (int i = 0; i < bank.getClientsCount(); i++) {
			System.out.println(BankService.getClientAccount(bank, i));
		}
	}
	
	/**
	 * @param bank the bank
	 * @param clientId the client id
	 * @param x the money
	 * @throws ClientNotFoundException 
	 * @throws NotEnoughFundsException 
	 */
	public static void withdrawClientAccount(Bank bank, int clientId, double x) throws ClientNotFoundException, NotEnoughFundsException {
		if (BankService.getClientAccount(bank, clientId) != null) {
			BankService.getClientAccount(bank, clientId).withdraw(x);
		} else {
			throw new ClientNotFoundException();
		}
	}
	
	/**
	 * @param bank the bank
	 * @param clientId the client id
	 * @param x the money
	 * @throws ClientNotFoundException 
	 */
	public static void depositClientAccount(Bank bank, int clientId, double x) throws ClientNotFoundException {
		if (BankService.getClientAccount(bank, clientId) != null) {
			BankService.getClientAccount(bank, clientId).deposit(x);
		} else {
			throw new ClientNotFoundException();
		}
	}
	
	/**
	 * @param bank the bank
	 * @param clientId the client id
	 * @return the maximum amount to withdraw
	 * @throws ClientNotFoundException 
	 */
	public static double getMaximumAmountToWithdraw(Bank bank, int clientId) throws ClientNotFoundException {
		if (BankService.getClientAccount(bank, clientId) != null) {
			return BankService.getClientAccount(bank, clientId).maximumAmountToWithdraw();
		} else {
			throw new ClientNotFoundException();
		}
	}
}
